<template>
  <div>
    <tips optionTemplate="classInfo"></tips>
    <div class="hearder-search">
      <div class="toplineBox mb-10">
        <div class="toplineBox-left">
          <el-button size="small" @click="refreshFn"><i class="el-icon-refresh"></i>刷新</el-button>
          <el-button size="small" @click="classId=0,classVisible=true" type="primary"><i class="el-icon-plus"></i>添加班级
          </el-button>
        </div>
        <p class="numberline toplineBox-right">总{{ page.total }}条</p>
      </div>
    </div>
    <div class="table-body">
      <div class="table-body-box">
        <el-table
          ref="multipleTable"
          :data="arryList"
          :stripe="tableStyle.tableStripe"
          :border="tableStyle.tableBorder"
          tooltip-effect="dark"
          style="width: 100%"
          header-cell-class-name="table-text-c"
          cell-class-name="table-text-c">
          <el-table-column
            prop="id"
            label="ID"
            width="80">
          </el-table-column>
          <el-table-column
            prop="section_id"
            label="年段">
          </el-table-column>
          <el-table-column
            prop="class_name"
            label="班级">
          </el-table-column>
          <el-table-column
            label="负责老师">
            <template slot-scope="scope">
                <span v-if="scope.row.user.length">
                <span v-for="(item,index) in scope.row.user">
                  {{ item.realname }}{{ index !== scope.row.user.length - 1 ? ',' : '' }}
                </span>
                  </span>
              <span v-else>未记录</span>
            </template>
          </el-table-column>
          <el-table-column
            label="归属"
            align="center">
            <template slot-scope="scope">
              {{ schoolData[scope.row.school_id] && schoolData[scope.row.school_id].name }}
            </template>
          </el-table-column>
          <el-table-column
            label="排序"
            align="center"
            prop="sort"
            width="80">
          </el-table-column>
          <el-table-column
            label="操作"
            width="260">
            <template slot-scope="scope">
              <el-button type="warning" size="small" @click="gotoQrcode(scope.row)" plain>区域二维码</el-button>
              <el-button type="primary" size="small" @click="classId=scope.row.id,classVisible=true" plain>详情
              </el-button>
              <el-button size="small" @click="detelTearch(1,scope.$index)" type="danger" plain>删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <qrcode_area :visible.sync="qrcodeVisible" :classoption="classoption"></qrcode_area>
    <classDetailPopover :visible.sync="classVisible" :classId="classId"></classDetailPopover>
  </div>
</template>

<script>
import addClass from '@/components/dialog/addClass.vue'
import {pagination, tableoption} from '@/utils/util.js'
import api from '@/utils/api.js'
import classDetailPopover from "./classDetailPopover";
import {schoolIndex} from '@/api/admin/system'

export default {
  components: {
    addClass,
    classDetailPopover
  },
  data() {
    return {
      classVisible: false,
      classId: 0,
      qrcodeVisible: false,
      handleSelection: [],
      addclassVisible: false,
      arryList: [], // 页面数据列表
      // 搜索设置
      searchWord: '', // 搜索名称
      // 设置table样式
      tableStyle: tableoption,
      // 设置table页面分页所需
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      isFirstEnter: false,
      classoption: {},
      schoolData: {}
    }
  },
  created() {
    this.schoolIndexFn()
    this.InitFn()
  },
  methods: {
    // 初始化
    InitFn() {
      this.$fetch(api.admin_classes_index, {
        p: this.page.pageindex
      }).then((response) => {
        if (response.code === 0) {
          this.arryList = response.list.data
          this.page.total = response.list.total
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    gotoQrcode(option) {
      this.classoption = JSON.parse(JSON.stringify(option))
      this.qrcodeVisible = true
    },
    checkIdCard(option) {
      this.editId = option || ''
      this.addclassVisible = true
    },
    // 分页
    handleSizeChange(val) {
      this.page.pageindex = val
      this.InitFn()
    },
    // 刷新页面
    refreshFn() {
      this.page.pageindex = 1
      this.InitFn()
    },
    detelTearch(type, index) {
      let name = '班级'
      // type 1 删除单个 2 批量删除
      if (type === 1) {
        name = this.arryList[index].section_id + '-' + this.arryList[index].class_name
      }
      this.$confirm('确认永久删除 ' + name + ' 信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.detelRespon(type, index)
      }).catch(() => {
      })
    },
    detelRespon(type, index) {
      // type 1 单独 2 批量
      console.log(type, index)
      let option_ = []
      let data_ = {}
      if (type === 2) {
        for (let i in this.handleSelection) {
          option_.push(this.handleSelection[i].id)
        }
        data_.id = option_.join(',')
      } else {
        data_.id = this.arryList[index].id
      }
      this.$post(api.admin_classes_detel, data_).then((response) => {
        if (response.code === 0) {
          this.InitFn()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    schoolIndexFn() {
      schoolIndex().then(res => {
        let schoolData_ = {
          0: {id: 0, name: '其他'}
        }
        for (let item of res.list) {
          schoolData_[item.id] = item
        }
        this.schoolData = schoolData_
      })
    },
  },
  // 缓存机制end
  mounted() {
  }
}
</script>
